using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using Neo.MetaModel;
using Webbness.Module.VsTool.Interfaces;

namespace Webbness.Module.VsTool
{
    [Serializable]
    public class MultiTemplateFileCreator : BaseTemplateCreator, IMultiTemplateCreator
    {
        protected Model _model;
        protected StringDictionary _properties = new StringDictionary();
        protected ArrayList _templates = new ArrayList();

        #region IMultiTemplateCreator Members

        public void Run()
        {
            if (!Directory.Exists(base._outputRoot))
            {
                Directory.CreateDirectory(base._outputRoot);
            }
            foreach (Entity entity in _model.Entities)
            {
                if (entity.Skip == false)
                {
                    string subDir;
                    ViewCodeGenerationContext ctx = new ViewCodeGenerationContext(entity);
                    if (entity.SubPackageName != null)
                    {
                        subDir = Path.Combine(base._outputRoot, entity.SubPackageName);
                    }
                    else
                    {
                        subDir = base._outputRoot;
                    }
                    subDir = Path.Combine(subDir, entity.ClassName);
                    if (!Directory.Exists(subDir))
                    {
                        Directory.CreateDirectory(subDir);
                    }
                    foreach (string viewType in _templates)
                    {
                        string outputFile = Path.Combine(subDir, (string.Empty + viewType) + ".aspx");
                        string templateName = new StringBuilder("NeoView").Append(viewType).Append(".vtl").ToString();
                        GenerateOutputFromTemplate(outputFile, templateName, ctx, entity.ForceOverwrite);
                        base._createdFiles.Add(outputFile);
                    }
                }
            }
        }

        public ArrayList CreatedFiles
        {
            get { return base._createdFiles; }
        }

        public bool CreateFolders
        {
            get { return base._createFolders; }
            set { base._createFolders = value; }
        }

        public bool ForceOverwrite
        {
            get { return base._forceOverwrite; }
            set { base._forceOverwrite = value; }
        }

        public Model Model
        {
            get { return _model; }
            set { _model = value; }
        }

        public string OutputFolder
        {
            get { return base._outputRoot; }
            set { base._outputRoot = value; }
        }

        public string ProjectName
        {
            get { return base._projectName; }
            set { base._projectName = value; }
        }

        public StringDictionary Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        public ArrayList Templates
        {
            get { return _templates; }
            set { _templates = value; }
        }

        public string TemplatesFolder
        {
            get { return base._templatesFolder; }
            set { base._templatesFolder = value; }
        }

        #endregion
    }
}